package com.kaoshi.agency.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaoshi.agency.domain.Qianyue;
import com.kaoshi.agency.service.QianyueService;
import com.kaoshi.common.config.Result;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/agency/qianyue")
public class QianyueController {
    @Autowired
    private QianyueService qianyueService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/findAll")
    public Result findAll(Integer pageNum, Integer pageSize, @RequestBody Qianyue qianyue){
        Page page = qianyueService.findAll(pageNum,pageSize,qianyue);
        return Result.ok(page);
    }
    @RequestMapping("/findAllJl")
    public Result findAllJl(Integer pageNum, Integer pageSize, @RequestBody Qianyue qianyue){
        Page page = qianyueService.findAllJl(pageNum,pageSize,qianyue);
        return Result.ok(page);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Qianyue qianyue = qianyueService.findById(id);
        return Result.ok(qianyue);
    }
    @RequestMapping("/update")
    public Result update(@RequestBody Qianyue qianyue){
        qianyue.setXiugaisj(LocalDateTime.now());
        qianyueService.updateById(qianyue);

        return Result.ok();
    }
    @RequestMapping("/updatesh")
    public Result updatesh(@RequestBody Qianyue qianyue){
        qianyue.setStatus(2);
        qianyue.setShenhesj(new Date());
        qianyue.setXiugaisj(LocalDateTime.now());
        qianyueService.updateById(qianyue);
        return Result.ok();
    }
    @RequestMapping("/updatebh")
    public Result updatebh(@RequestBody Qianyue qianyue){
        qianyue.setStatus(3);
        qianyue.setXiugaisj(LocalDateTime.now());
        qianyueService.updateById(qianyue);
        return Result.ok();
    }
    @RequestMapping("/updatexy")
    public Result updatexy(@RequestBody Qianyue qianyue){
        qianyue.setStatus(2);
        qianyue.setXiugaisj(LocalDateTime.now());
        qianyueService.updateById(qianyue);
        return Result.ok();
    }
    //导出
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("签约列表", "UTF-8").replaceAll("/+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        //2.查询要导出的数据
        List<Qianyue> studentList= qianyueService.findAll2();

        //3.使用EasyExcel完成导出
        EasyExcel.write(response.getOutputStream())
                .head(Qianyue.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("签约列表")
                .doWrite(studentList);

    }
    @RequestMapping("/pay")
    public Result pay(Integer id){
        Qianyue qianyue = qianyueService.findById(id);
        qianyue.setStatus(4);
        qianyue.setXiugaisj(LocalDateTime.now());
        qianyueService.updateById(qianyue);
        return Result.ok();
    }
    @RequestMapping("/tixing")
    public Result tixing(Integer id){
        String nr = "您的签约信息已通过审核，请尽快完成支付";
        redisTemplate.opsForValue().set("tixing"+id, JSON.toJSONString(nr));
        return Result.ok();
    }


    @RequestMapping("/getByBh/{bh}")
    public List<Qianyue> getByBh(@PathVariable Integer bh){
        List<Qianyue> qianyues = qianyueService.getByBh(bh);
        return qianyues;
    }

}
